import { Message } from 'element-ui'
import { getToken, setToken } from '@/utils/auth'
import { getUserinfo, login, getEmployeesInfo } from '@/api/user'
import { resetRouter } from '@/router'
export default {
  namespaced: true,
  state: () => {
    return {
      token: getToken() || '',
      userInfo: {}
    }
  },
  mutations: {
    getToken(state, newToken) {
      state.token = newToken
      setToken(newToken)
    },
    getInfo(state, newInfo) {
      state.userInfo = newInfo
    }
  },
  actions: {
    // 登录功能
    async login(context, data) {
      const res = await login(data)
      context.commit('getToken', res.data)
      Message.success(res.message)
    },
    // 获取用户基本信息
    async getUserinfo(context) {
      const res = await getUserinfo()
      // 获取员工基本信息  (头像)
      const resPhone = await getEmployeesInfo(res.data.userId)
      context.commit('getInfo', { ...res.data, ...resPhone.data })
      return res.data.roles.menus
    },
    // 清空token和基本信息
    delInfo(context) {
      context.commit('getToken', '')
      context.commit('getInfo', {})

      resetRouter()
    }
  }
}
